package com.opencee.common.utils;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * 国际化工具类
 *
 * @author liuyadu
 * @date 2021/12/31
 */
public class LocaleUtil {
    /**
     * 获取i18
     *
     * @param basename    国际化文件名路径 messages,
     * @param messageCode
     * @param args
     * @return
     */
    public static String getMessage(String basename, String messageCode, Object... args) {
        try {
            ResourceBundleMessageSource source = new ResourceBundleMessageSource();
            source.setBasename(basename);
            return source.getMessage(messageCode, args, messageCode, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            e.printStackTrace();
            return messageCode;
        }
    }

    /**
     * 获取i18n消息
     *
     * @param messageCode
     * @param args
     * @return
     */
    public static String getMessage(String messageCode, Object... args) {
        return getMessage("messages", messageCode, args);
    }

}
